package com.example.jiwhsgs;

import android.app.Activity;
import android.app.AlertDialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SMSactivity extends Activity {
    private SQLiteDatabase myDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myDatabase = openOrCreateDatabase("sgsdb.db", MODE_PRIVATE, null);
        List<XSactivity.XSData> smsData = getData("emperor");
        int length = smsData.size();


        List<Integer> indices = new ArrayList<>();
        for (int i = 0; i < length; i++)
            indices.add(i);

        Collections.shuffle(indices);

        int random1 = indices.get(0);
        int random2 = indices.get(1);
        int random3 = indices.get(2);

        String messageBuilder =
                "1." + smsData.get(random1).name + ": " + smsData.get(random1).detail + "\n" +
                "2." + smsData.get(random2).name + ": " + smsData.get(random2).detail + "\n" +
                "3." + smsData.get(random3).name + ": " + smsData.get(random3).detail + "\n";

        showAlertDialog(messageBuilder);
    }

    private void showAlertDialog(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.CustomDialog);
        builder.setTitle("主公技");

        // 创建 SpannableString
        SpannableString spannableMessage = new SpannableString(message);

        // 设置统一的文本大小
        spannableMessage.setSpan(new AbsoluteSizeSpan(20, true), 0, message.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        // 设置特定部分的文本颜色
        int startIndex1 = message.indexOf("1.") + 2; // 假设 "1." 后面的文字是要改变颜色的部分
        int endIndex1 = message.indexOf(":", startIndex1);
        spannableMessage.setSpan(new ForegroundColorSpan(Color.RED), startIndex1, endIndex1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        int startIndex2 = message.indexOf("2.") + 2;
        int endIndex2 = message.indexOf(":", startIndex2);
        spannableMessage.setSpan(new ForegroundColorSpan(Color.RED), startIndex2, endIndex2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        int startIndex3 = message.indexOf("3.") + 2;
        int endIndex3 = message.indexOf(":", startIndex3);
        spannableMessage.setSpan(new ForegroundColorSpan(Color.RED), startIndex3, endIndex3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        // 设置 detail 部分的文本颜色为黑色
        int detailStartIndex1 = endIndex1 + 2; // 假设 "1." 后面的文字是要改变颜色的部分
        int detailEndIndex1 = message.indexOf("\n", detailStartIndex1);
        spannableMessage.setSpan(new ForegroundColorSpan(Color.BLACK), detailStartIndex1, detailEndIndex1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        int detailStartIndex2 = endIndex2 + 2;
        int detailEndIndex2 = message.indexOf("\n", detailStartIndex2);
        spannableMessage.setSpan(new ForegroundColorSpan(Color.BLACK), detailStartIndex2, detailEndIndex2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        int detailStartIndex3 = endIndex3 + 2;
        int detailEndIndex3 = message.indexOf("\n", detailStartIndex3);
        spannableMessage.setSpan(new ForegroundColorSpan(Color.BLACK), detailStartIndex3, detailEndIndex3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        builder.setMessage(spannableMessage);
        builder.setPositiveButton("确定", (dialog, which) -> dialog.dismiss());
        builder.show();
    }

    private List<XSactivity.XSData> getData(String tableName) {
        List<XSactivity.XSData> skillList = new ArrayList<>();
        String query = "SELECT * FROM " + tableName;
        try (Cursor cursor = myDatabase.rawQuery(query, null)) {
            if (cursor != null && cursor.moveToFirst()) {
                int nameIndex = cursor.getColumnIndex("name");
                int detailIndex = cursor.getColumnIndex("detail");

                do {
                    XSactivity.XSData skillData = new XSactivity.XSData();
                    skillData.name = cursor.getString(nameIndex);
                    skillData.detail = cursor.getString(detailIndex);
                    skillData.is_selected = false;
                    skillList.add(skillData);
                } while (cursor.moveToNext());
            }
        }
        return skillList;
    }
}
